package org.jash.shop2212;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class GoodsAdapter extends BaseAdapter {
    private final List<GoodsEntity.DataDTO> list = new ArrayList<>();
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public GoodsEntity.DataDTO getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
            convertView.setTag(new ViewHolder(convertView));
        }
        GoodsEntity.DataDTO dto = list.get(position);
        ViewHolder holder = (ViewHolder) convertView.getTag();
        holder.title.setText(dto.getGoodsDesc());
        holder.price.setText(dto.getGoodsPrice() + "元");
        Glide.with(holder.icon)
                .load(dto.getGoodsIcon())
                .into(holder.icon);
        return convertView;
    }
    public void addAll(List<GoodsEntity.DataDTO> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }
    public static class ViewHolder {
        private final TextView title, price;
        private final ImageView icon;

        public ViewHolder(View itemView) {
            title = itemView.findViewById(R.id.title);
            price = itemView.findViewById(R.id.price);
            icon = itemView.findViewById(R.id.icon);
        }
    }
}
